package com.yanqu.road.dao.impl.activity.commonsupport;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.commonsupport.CommonUnionSupportUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonUnionSupportUserDataDaoImpl extends TempDataObjectDao<CommonUnionSupportUserData> {

    public static String getCommonUnionSupportUserDataSql = "select * from `t_c_common_union_support_user` where `activityId`=? and `groupId`=?";
    public Map<Long, CommonUnionSupportUserData> getCommonUnionSupportUserData(int activityId,int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CommonUnionSupportUserData>  map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCommonUnionSupportUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonUnionSupportUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCommonUnionSupportUserDataSql = "update `t_c_common_union_support_user` set unionUid=?,getReward=? where activityId=? and groupId=? and userId=?";
    @Override
    protected boolean updateData(CommonUnionSupportUserData commonUnionSupportUserData) {
        commonUnionSupportUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, commonUnionSupportUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportUserData.getGetReward()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonUnionSupportUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCommonUnionSupportUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCommonUnionSupportUserDataSql = "insert into `t_c_common_union_support_user`(activityId,groupId,userId,unionUid,getReward) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CommonUnionSupportUserData commonUnionSupportUserData) {
        commonUnionSupportUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonUnionSupportUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonUnionSupportUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, commonUnionSupportUserData.getGetReward()));
        if (getDb().executeNoneQuery(insertCommonUnionSupportUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CommonUnionSupportUserData parseData(ResultSet rs) throws SQLException {
        CommonUnionSupportUserData commonUnionSupportUserData = new CommonUnionSupportUserData();
        commonUnionSupportUserData.setActivityId(rs.getInt("activityId"));
        commonUnionSupportUserData.setGroupId(rs.getInt("groupId"));
        commonUnionSupportUserData.setUserId(rs.getLong("userId"));
        commonUnionSupportUserData.setUnionUid(rs.getString("unionUid"));
        commonUnionSupportUserData.setGetReward(rs.getInt("getReward"));

        return commonUnionSupportUserData;
    }
}
